/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Page分页
 *
 * @author tianyuliang
 * @version $Id: Page.java, v0.1 2018/10/15
 */
@SuppressWarnings("unchecked")
public class Page<T> implements Serializable {

    private static final long serialVersionUID = 5636079738558757591L;

    /**
     * 页宽
     */
    @Getter
    private int pageSize = 20;
    /**
     * 页码
     */
    @Getter
    private int current = 1;
    /**
     * 总条数
     */
    @Getter
    private int total;
    /**
     * 总页数
     */
    @Getter
    private int pageCount;
    /**
     * 数据
     */
    @Getter
    @Setter
    private List<T> list;

    /**
     * 构造方法
     */
    public Page() {
        this.total = 0;
        this.list = new ArrayList<>();
    }

    /**
     * 构造方法
     *
     * @param current
     * @param pageSize
     * @param total
     */
    public Page(Integer current, Integer pageSize, int total) {
        this.setCurrent(current);
        this.setPageSize(pageSize);
        this.setTotal(total);
        this.list = new ArrayList<>();
    }

    /**
     * 构造方法
     *
     * @param current
     * @param pageSize
     * @param total
     * @param data
     */
    public Page(Integer current, Integer pageSize, int total, List<T> data) {
        this.setCurrent(current);
        this.setPageSize(pageSize);
        this.setTotal(total);
        this.setList(data);
    }

    /**
     * 构造方法
     *
     * @param condition
     * @param total
     * @param data
     */
    public Page(PageCondition condition, int total, List<T> data) {
        this(condition.getPageNum(), condition.getPageSize(), total, data);
    }

    /**
     * 空Page分页
     *
     * @param limit
     * @param <T>
     * @return
     */
    public static <T> Page<T> ofEmpty(int limit) {
        return new Page(1, limit, 0);
    }

    /**
     * 偏移量
     *
     * @return
     */
    public int getOffset() {
        return this.getCurrent() * this.getPageSize();
    }

    /**
     * 第一页
     *
     * @return
     */
    public int getFirstPage() {
        return 1;
    }

    /**
     * 最后一页
     *
     * @return
     */
    public int getLastPage() {
        return this.getPageCount();
    }

    /**
     * 下一页
     *
     * @return
     */
    public int getNextPage() {
        return this.getCurrent() + 1 > this.getPageCount() ? this.getPageCount() : this.getCurrent() + 1;
    }

    /**
     * 上一页
     *
     * @return
     */
    public int getPrevPage() {
        return this.getCurrent() - 1 < 1 ? 1 : this.getCurrent() - 1;
    }

    /**
     * 设置每页条数
     *
     * @param pageSize
     */
    public void setPageSize(Integer pageSize) {
        if (pageSize != null && pageSize > 0) {
            this.pageSize = pageSize;
        }
    }

    /**
     * 设置页码
     *
     * @param current
     */
    public void setCurrent(Integer current) {
        if (current == null || current < 1) {
            current = 1;
        }
        if (this.getPageCount() > 0 && current >= this.getPageCount()) {
            if (this.getPageCount() > 0) {
                current = this.getPageCount();
            }
        }
        this.current = current;
    }

    /**
     * 设置总条数
     *
     * @param total
     */
    public void setTotal(int total) {
        this.total = total;
        this.pageCount = (this.total + this.pageSize - 1) / this.pageSize;
    }

    /**
     * 获取分页信息
     *
     * @return
     */
    @JsonIgnore
    public String getPageInfo() {
        String format = "getOffset=%d, getFirstPage=%d, getPrevPage=%d, getNextPage=%d, getLastPage=%d, total=%d, pageSize=%d, pageCount=%d, current=%d";
        return String.format(format, getOffset(), getFirstPage(), getPrevPage(), getNextPage(), getLastPage(), getTotal(), getPageSize(), getPageCount(), getCurrent());
    }

}
